<?php
namespace App\Util;

use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class ThumbHelper {
    /**
     * 生成缩略图
     * @author gaoyoulong
     * @param $fileName 文件名
     * @param $originalPrefixPath 原图路径前缀
     * @param $thumbPrefixPath 缩略图路径前缀
     * @param $sizeArr 缩略图大小 exp:[100,100]
     */
    public static function makeThumbPic($fileName,$thumbPrefixPath=null,$sizeArr=null,$originalPrefixPath=null){
        if(empty($originalPrefixPath)){
            $originalPrefixPath = config('image.originalPicPath');
        }
        if(empty($thumbPrefixPath)){
            $thumbPrefixPath = config('image.smallPicPath');
        }
        $filePath = config('filesystems.disks.local.root').DIRECTORY_SEPARATOR.$originalPrefixPath.DIRECTORY_SEPARATOR.$fileName;  //原始路径
        //生成缩略图
        if(empty($sizeArr)){
            $sizeArr = config('image.smallPicSize');  //缩略图大小
        }
        //生成缩略图
        $image = Image::make($filePath)->resize($sizeArr[0],null, function ($constraint) {
            $constraint->aspectRatio();//高度auto
        });

        $smallPicPath = config('filesystems.disks.local.root').DIRECTORY_SEPARATOR.$thumbPrefixPath.DIRECTORY_SEPARATOR.$fileName;  //缩略图路径

        //判断路径
        $dir = dirname($smallPicPath);
        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }

        $image->save($smallPicPath);
    }
}